package org.usmile.algorithms.huawei.acm;

import java.util.Scanner;

// https://blog.csdn.net/amos_cloud/article/details/129097199
public class _区块链文件转储系统 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = Integer.parseInt(in.nextLine());
        String[] dataStr = in.nextLine().split(" ");

        int[] data = new int[dataStr.length];
        for (int i = 0; i < data.length; i++) {
            data[i] = Integer.parseInt(dataStr[i]);
        }

        int windowCapacity = 0;
        int left = 0;
        int right = 0;
        int max = 0;
        while (right < data.length) {
            windowCapacity += data[right];
            while (windowCapacity > m) {
                windowCapacity -= data[left];
                left++;
            }
            max = Math.max(max, windowCapacity);
            right ++;
        }

        System.out.println(max);
    }
}
